package com.lanjiu.lib.business;

import com.lanjiu.lib.app.App;
import com.lanjiu.lib.business.listener.FileTransmissionListener;
import com.lanjiu.lib.business.option.FileTransmissionChannel;
import com.lanjiu.lib.business.task.DownloadTask;
import com.lanjiu.lib.business.task.UploadTask;
import com.lanjiu.lib.business.work.FileTransmissionExecutor;
import com.lanjiu.lib.utils.NetUtils;
import com.lanjiu.pro.file.OriginalPictureVideoFriendsTransmission;
import com.lanjiu.pro.file.OriginalPictureVideoGroupTransmission;

/**
 * @description: 文件下载管理器
 * @author: Panwei
 * @date: 2019/11/1 16:46
 */
public class FileTransmissionManager {

    private static FileTransmissionManager mInstance;

    private FileTransmissionExecutor mFileTransmissionExecutor;

    private FileTransmissionManager() {
        mFileTransmissionExecutor = new FileTransmissionExecutor();
        mFileTransmissionExecutor.initFileTransmissionExecutor();
    }

    public static FileTransmissionManager getInstance() {
        if (mInstance == null) {
            synchronized (FileTransmissionManager.class) {
                if (mInstance == null) {
                    // 使用双重同步锁
                    mInstance = new FileTransmissionManager();
                }
            }
        }
        return mInstance;
    }

    public Downloader buildDownloader() {
        return new Downloader(mFileTransmissionExecutor);
    }

    public Uploader buildUploader() {
        return new Uploader(mFileTransmissionExecutor);
    }

    // 下载 Builder
    public class Downloader {
        private FileTransmissionExecutor mFileTransmissionExecutor;
        private String mTargetPath;
        private String mServerPath;
        private FileTransmissionListener.OnFileReceiverListener mFileReceiverListener;

        private OriginalPictureVideoFriendsTransmission mFriend;
        private OriginalPictureVideoGroupTransmission mGroup;
        private boolean isGroup;

        private Downloader(FileTransmissionExecutor executor) {
            this.mFileTransmissionExecutor = executor;
        }

        public Downloader targetPath(String targetPath) {
            this.mTargetPath = targetPath;
            return this;
        }

        public Downloader serverPath(String serverPath) {
            this.mServerPath = serverPath;
            return this;
        }

        public Downloader onFileReceiver(FileTransmissionListener.OnFileReceiverListener listener) {
            this.mFileReceiverListener = listener;
            return this;
        }

        public Downloader friend(OriginalPictureVideoFriendsTransmission friend) {
            this.isGroup = false;
            this.mFriend = friend;
            return this;
        }

        public Downloader group(OriginalPictureVideoGroupTransmission group) {
            this.isGroup = true;
            this.mGroup = group;
            return this;
        }

        public void download() {
            if (mGroup == null && mFriend == null)
                throw new NullPointerException("Please call the 'friend()' or 'group()' before download");
            if (!NetUtils.isConnected(App.getContext())) {
                if (mFileReceiverListener != null)
                    mFileReceiverListener.onError(TransmissionConstant.TRANSMISSION_TASK_NOT_NETWORK, "请检查网络设置");
                return;
            }
            DownloadTask task;
            if (isGroup)
                task = DownloadTask.newTask(mTargetPath, mServerPath, mFileReceiverListener, mGroup);
            else
                task = DownloadTask.newTask(mTargetPath, mServerPath, mFileReceiverListener, mFriend);

            if (task != null) {
                this.mFileTransmissionExecutor.execFileTransmissionTask(task);
            } else {
                if (mFileReceiverListener != null)
                    mFileReceiverListener.onError(TransmissionConstant.TRANSMISSION_TASK_REPET, "下载任务已存在");
            }
        }
    }

    // 上传 Builder
    public class Uploader {
        private FileTransmissionExecutor mFileTransmissionExecutor;
        private String mTargetPath;
        private FileTransmissionListener.OnFileSenderListener mFileSenderListener;

        private OriginalPictureVideoFriendsTransmission mFriend;
        private OriginalPictureVideoGroupTransmission mGroup;
        private boolean isGroup;

        private Uploader(FileTransmissionExecutor executor) {
            this.mFileTransmissionExecutor = executor;
        }

        public Uploader targetPath(String targetPath) {
            this.mTargetPath = targetPath;
            return this;
        }

        public Uploader onFileReceiver(FileTransmissionListener.OnFileSenderListener listener) {
            this.mFileSenderListener = listener;
            return this;
        }

        public Uploader friend(OriginalPictureVideoFriendsTransmission friend) {
            this.isGroup = false;
            this.mFriend = friend;
            return this;
        }

        public Uploader group(OriginalPictureVideoGroupTransmission group) {
            this.isGroup = true;
            this.mGroup = group;
            return this;
        }

        public void upload() {
            if (mGroup == null && mFriend == null)
                throw new NullPointerException("Please call the 'friend()' or 'group()' before upload");
            if (!NetUtils.isConnected(App.getContext())) {
                if (mFileSenderListener != null)
                    mFileSenderListener.onError(TransmissionConstant.TRANSMISSION_TASK_NOT_NETWORK, "请检查网络设置");
                return;
            }
            UploadTask task;
            if (isGroup)
                task = UploadTask.newTask(mTargetPath, mFileSenderListener, mGroup);
            else
                task = UploadTask.newTask(mTargetPath, mFileSenderListener, mFriend);

            if (task != null) {
                this.mFileTransmissionExecutor.execFileTransmissionTask(task);
            } else {
                if (mFileSenderListener != null)
                    mFileSenderListener.onError(TransmissionConstant.TRANSMISSION_TASK_REPET, "上传任务已存在");
            }
        }
    }

    public static void shutdown() {
        FileTransmissionChannel.shutdownAll();
        if (mInstance != null) {
            if (mInstance.mFileTransmissionExecutor != null) {
                mInstance.mFileTransmissionExecutor.destroyFileTransmissionExecutor();
                mInstance.mFileTransmissionExecutor = null;
            }
        }
        mInstance = null;
    }

    public static boolean isShutdown() {
        if (mInstance != null) {
            if (mInstance.mFileTransmissionExecutor != null) {
                return mInstance.mFileTransmissionExecutor.isShutdown();
            }
        }
        return true;
    }
}
